#!/bin/bash
# --------------------------------------------------------------------------------------------------
# Morningstar_Build_Controller.sh
#
# This backend bash script manages the dispatch of all Morningstar build jobs that run in Stata
# --------------------------------------------------------------------------------------------------

# INPUT ENVIRONMENT VARIABLES
echo "USER="${1}
echo "STEP="${2}
echo "START_YEAR="${3}
echo "END_YEAR="${4}

# OUTPUT ENVIRONMENT VARIABLES
echo "SLURM_JOB_ID="$SLURM_JOB_ID
echo "SLURM_JOB_NODELIST"=$SLURM_JOB_NODELIST
echo "SLURM_NNODES"=$SLURM_NNODES
echo "SLURM_SUBMIT_DIR="$SLURM_SUBMIT_DIR
echo "SLURM_ARRAY_TASK_ID="$SLURM_ARRAY_TASK_ID
echo "SLURM_ARRAY_JOB_ID"=$SLURM_ARRAY_JOB_ID
echo "SLURM_NTASKS"=$SLURM_NTASKS

# LOAD SHELL PROFILE
if [ -f ~/shell_profile.sh ];
then
    source ~/shell_profile.sh
else
    echo "Please set up ~/shell_profile.sh file. Exiting."
    exitcd
fi

# RUN CALCULATIONS
umask 007
stata-mp -b "${code_path}/morningstar/Morningstar_Build.do" ${1} ${2} ${3} ${4} ${SLURM_ARRAY_TASK_ID}

# FINISHED
echo "Finished Step "${2}
exit
